﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Accountings
{
	public partial class eCdsTransInquiry : WeifenLuo.WinFormsUI.Docking.DockContent  
	{
		public eCdsTransInquiry()
		{
			InitializeComponent();
		}
		
		private void eCdsTransInquiry_Load(object sender, EventArgs e)
		{
			try
			{
                ABSCommonLibrary.CreateGridView(ref gridControl1);
                DataSet tran_type = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 2, 0, 0 });
                DataSet tran_type1 = tran_type.Copy();
                comboBoxEditor1.SetDataBinding(tran_type1, tran_type1.Tables[0].TableName);
                comboBoxEditor1.ValueMember = "CODE";
                comboBoxEditor1.DisplayFormat = "%DESCRIPTION%";                
                gridControl1.Columns["TRANS_TYPE"].CellEditorManager = comboBoxEditor1;

                comboBoxViewer1.SetDataBinding(tran_type, tran_type.Tables[0].TableName);
                comboBoxViewer1.ValueMember = "CODE";
                comboBoxViewer1.DisplayFormat = "%DESCRIPTION%";
                gridControl1.Columns["TRANS_TYPE"].CellViewerManager = comboBoxViewer1;

                colTRANS_DATE.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
                colQUANTITY.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
                colDATE_CREATED.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;

                switch (this.Tag.ToString())
                {
                    case "00":
                        contextMenuStrip1.Enabled = false;
                        valueRow1.Visible = false;
                        break;
                    case "01":
                        contextMenuStrip1.Enabled = false;
                        insertionRow1.Visible = false;
                        break;
                        
                    case "02":
                        insertionRow1.Visible = false;
                        break;
                    default:
                        break;
                }
			}
			catch (Exception ex)
			{
				ABSCommonLibrary.ShowErrorMessage(ex.ToString());
			}
		}

		private void load_grid()
		{
            DateTime trans_date = DeclareLibrary.MARKET_DAY;
            string account_no = string.Empty;
            string stock_code = string.Empty;
            string status = string.Empty;
            int tran_type = 0;
            if (valueRow1.Cells["ACCOUNT_NO"].Value != null)
                account_no = valueRow1.Cells["ACCOUNT_NO"].Value.ToString();
            if (valueRow1.Cells["CODE"].Value != null)
                stock_code = valueRow1.Cells["CODE"].Value.ToString();
            if (valueRow1.Cells["TRANS_DATE"].Value != null)
                trans_date = Convert.ToDateTime(valueRow1.Cells["TRANS_DATE"].Value);
            if (valueRow1.Cells["STATUS"].Value != null)
                status = valueRow1.Cells["STATUS"].Value.ToString();
            if (valueRow1.Cells["TRANS_TYPE"].Value != null)
                tran_type = Convert.ToInt16(valueRow1.Cells["TRANS_TYPE"].Value);
            DataSet v_data = ABSCommonLibrary.DoInquiry(
                CommonLibrary.CommonLibrary.BOSC015001, new object[5]{
                    trans_date, stock_code, tran_type, account_no, status});
            gridControl1.SetDataBinding(v_data, v_data.Tables[0].TableName);

		}

		private void approve_cds_trans()
		{
			if (gridControl1.DataRows.Count == 0)
				return;

            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                    " " + gridControl1.CurrentCell.ParentRow.Cells["QUANTITY"].Value.ToString() +
                    " chung khoan " + gridControl1.CurrentCell.ParentRow.Cells["CODE"].Value.ToString() + " cua tai khoan " +
                    gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();
			if (ABSCommonLibrary.ShowQuestion(
                "Ban muon xac nhan giao dich " + sQuestion + "?") == DialogResult.No)
				return;
			
			object[] p_param = new object[3];
            bool bSuccess = false;
            
            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
			p_param[1] = DeclareLibrary.USER_NAME;
			p_param[2] = DeclareLibrary.MARKET_DAY;
            bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(
                CommonLibrary.CommonLibrary.BOSC015003, ref p_param));
			if (bSuccess)			
				ABSCommonLibrary.ShowMessage("Xac nhan giao dich " + sQuestion + "thanh cong!");			
			else			
				ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " khong thanh cong!");			
			load_grid();
		}
        
		private void cmsApprove_Click(object sender, EventArgs e)
		{
			try
			{
				approve_cds_trans();				
			}
			catch (Exception ex)
			{
				ABSCommonLibrary.ShowErrorMessage(ex.ToString());
			}
		}
		private void cancel_cds_trans()
		{
            if (gridControl1.DataRows.Count == 0)
				return;

            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                    " " + gridControl1.CurrentCell.ParentRow.Cells["QUANTITY"].Value.ToString() +
                    " chung khoan " + gridControl1.CurrentCell.ParentRow.Cells["CODE"].Value.ToString() + " cua tai khoan " +
                    gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();

            if (gridControl1.CurrentCell.ParentRow.Cells["STATUS"].Value.ToString() != "C")
            {
                ABSCommonLibrary.ShowMessage("Khong duoc phep huy giao dich " 
                    + sQuestion + " do giao dich nay chua duoc xac nhan!");
                return;
            }

			if (ABSCommonLibrary.ShowQuestion("Ban muon huy giao dich " + sQuestion + "?") == DialogResult.No)
				return;

			object[] p_param = new object[3];
            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
			p_param[1] = DeclareLibrary.USER_NAME;
			p_param[2] = DeclareLibrary.USER_NAME;
			bool bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(CommonLibrary.CommonLibrary.BOSC015006, ref p_param));
			if (bSuccess)
			    ABSCommonLibrary.ShowMessage("Huy giao dich " + sQuestion + "thanh cong!");			
			else			
				ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " khong thanh cong!");
			
			load_grid();
		}
		
        private void adjust_cds_trans()
        {
            if (gridControl1.DataRows.Count == 0)
                return;
            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                    " " + gridControl1.CurrentCell.ParentRow.Cells["QUANTITY"].Value.ToString() +
                    " chung khoan " + gridControl1.CurrentCell.ParentRow.Cells["CODE"].Value.ToString() + " cua tai khoan " +
                    gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();
            if (gridControl1.CurrentCell.ParentRow.Cells["STATUS"].Value.ToString() != "P")
            {
                ABSCommonLibrary.ShowMessage("Khong duoc phep hieu chinh giao dich "
                    + sQuestion + " do giao dich nay da duoc xac nhan!");
                return;
            }

            if (ABSCommonLibrary.ShowQuestion("Ban muon hieu chinh giao dich " + sQuestion + "?") == DialogResult.No)
                return;

            object[] p_param = new object[3];
            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
            p_param[1] = DeclareLibrary.USER_NAME;
            p_param[2] = DeclareLibrary.USER_NAME;
            bool bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(CommonLibrary.CommonLibrary.BOSC015008, ref p_param));
            if (bSuccess)            
                ABSCommonLibrary.ShowMessage("Hieu chinh giao dich " + sQuestion + "thanh cong!");            
            else            
                ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " khong thanh cong!");
           
            load_grid();
        }

        private void cmsCancel_Click(object sender, EventArgs e)
        {
            try
            {
                cancel_cds_trans();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void cmsAdjust_Click(object sender, EventArgs e)
        {
            try
            {
                adjust_cds_trans();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
        private void reject_cdstrans()
        {
            if (gridControl1.DataRows.Count == 0)
                return;
            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                     " " + gridControl1.CurrentCell.ParentRow.Cells["QUANTITY"].Value.ToString() +
                     " chung khoan " + gridControl1.CurrentCell.ParentRow.Cells["CODE"].Value.ToString() + " cua tai khoan " +
                     gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();
            if (ABSCommonLibrary.ShowQuestion("Ban muon xoa giao dich " + sQuestion + "?") == DialogResult.No)
                return;
            if (gridControl1.CurrentCell.ParentRow.Cells["STATUS"].Value.ToString() != "P")
            {
                ABSCommonLibrary.ShowMessage("Giao dich da duoc xac nhan, khong duoc phep xoa");
                return;
            }

            object[] p_param = new object[2];

            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
            p_param[1] = DeclareLibrary.USER_NAME;

            bool bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(CommonLibrary.CommonLibrary.BOSC015007, ref p_param));
            if (bSuccess)           
                ABSCommonLibrary.ShowMessage("Xoa giao dich " + sQuestion + "thanh cong!");           
            else           
                ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " xoa khong thanh cong!");
            
            load_grid();
        }
        private void cmsReject_Click(object sender, EventArgs e)
        {
            try
            {
                reject_cdstrans();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void eCdsTransInquiry_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F9)
                {
                    if (((DataSet)gridControl1.DataSource).Tables[0].GetChanges() != null)
                    {
                        if (ABSCommonLibrary.ShowQuestion(
                            "Bo qua nhung thay doi giao dich vua duoc tao?") == DialogResult.No)
                            return;
                    }
                    load_grid();
                }
                 else if (e.KeyCode == Keys.F11)
                {
                    if (((DataSet)gridControl1.DataSource).Tables[0].GetChanges() != null)
                    {
                        //CenturyFox.Service data_access = new CenturyFox.Service();
                        foreach (DataRow row in ((DataSet)gridControl1.DataSource).Tables[0].GetChanges().Rows)
                        {
                            if (row.RowState == DataRowState.Added)
                            {
                                object[] p_param = new object[9];
                                p_param[0] = row["TRANS_DATE"]; 
                                p_param[1] = DeclareLibrary.MARKET_DAY;
                                p_param[2] = row["TRANS_TYPE"]; 
                                p_param[3] = row["QUANTITY"];
                                p_param[4] = row["CODE"];
                                p_param[5] = row["ACCOUNT_NO"];
                                if (row["NOTES"] == DBNull.Value)
                                    p_param[6] = string.Empty;
                                else
                                    p_param[6] = row["NOTES"];

                                p_param[8] = DeclareLibrary.MARKET_DAY;
                                p_param[7] = DeclareLibrary.USER_NAME;
                                if (Convert.ToInt32(ABSCommonLibrary.DoExecute(
                                    CommonLibrary.CommonLibrary.BOSC015002, ref p_param)) == 0)
                                {
                                    ABSCommonLibrary.ShowMessage("Giao dich luu ky da duoc thuc hien");                                        
                                }
                                else
                                {
                                    ABSCommonLibrary.ShowMessage("Chung khoan hoac tai khoan nha dau tu khong ton tai");
                                }
                            }
                        }
                        load_grid();
                    }

                }
                else if (e.KeyCode == Keys.F10)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                    saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
                    saveFileDialog.AddExtension = true;
                    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                    {
                        string FileName = saveFileDialog.FileName;
                        // TODO: Add code here to save the current contents of the form to a file.
                        Xceed.Grid.Exporting.ExcelExporter export = new Xceed.Grid.Exporting.ExcelExporter();
                        export.Export(gridControl1, FileName);
                    }
                }
                    
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
	}
}